<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架 [ 以下开发工具自动生成-版权勿删 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;
use app\common\model\BaseModel;

/**
 * 系统框架基础模型
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 */
class AdminBaseModel extends BaseModel
{
    // 定义全局的查询范围
    protected $globalScope = ['default'];

    /**
     * 增加修改全局查询范围属性函数
     * Create by :  刘泓宾<514660094@qq.com>
     * @param $scope
     * @return $this
     */
    public function setGlobalScope($scope = ['alias'])
    {
        $this->globalScope = $scope;
        return $this;
    }

    /**
     * 定义自定义默认全局查询范围
     * Create by :  刘泓宾<514660094@qq.com>
     * @param $query \think\Model
     * @return void
     */
    public function scopeAlias($query)
    {
        if (get_plaid()) {
            $query->where([$this->alias . '.plaid' => get_plaid()]);
        }

        if (get_appid()) {
            $query->where([$this->alias . '.appid' => get_appid()]);
        }
    }


    /**
     * 定义查询范围
     * Create by :  刘泓宾<514660094@qq.com>
     * @param $query
     * @return void
     */
    public function scopeDefault($query)
    {
        // 添加平台ID为查询条件
        if (get_plaid()) {
            $query->where(['plaid' => get_plaid()]);
        }

        // 添加小程序ID为查询条件
        if (get_appid()) {
            $query->where(['appid' => get_appid()]);
        }
    }

    /**
     * 数据新增前执行
     * Create by :  刘泓宾<514660094@qq.com>
     * @param Model $model
     * @return void
     */
    public static function onBeforeInsert(Model $model)
    {
        if (get_appid()) {
            $model->appid = get_appid();
        }
        if (get_plaid()) {
            $model->plaid = get_plaid();
        }
    }
}
